<template>
  <div>
    <!-- 内容管理 -->
    <el-card class="box-card">
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to='/'>首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
        <el-form label-width="80px">
          <el-form-item label="状态：">
            <el-radio-group v-model="reqParams.status">
              <!-- 文章状态，0-草稿，1-待审核，2-审核通过，3-审核失败，不传为全部 -->
              <el-radio :label="null">全部</el-radio>
              <el-radio :label="0">草稿</el-radio>
              <el-radio :label="1">待审核</el-radio>
              <el-radio :label="2">审核通过</el-radio>
              <el-radio :label="3">审核失败</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="频道：">
            <GeekChannel v-model="reqParams.channel_id"/>
          </el-form-item>
          <el-form-item label="日期：">
            <div class="block">
              <el-date-picker v-model="dateArr" type="daterange" range-separator="至" start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="sift">筛选</el-button>
          </el-form-item>
          {{ reqParams }}
        </el-form>
      </div>
    </el-card>
    <!-- 筛选 -->
    <el-card>
      <div slot="header">
        根据筛选条件共查询到 {{ totalCount }} 条结果
      </div>

      <el-table :data="articleList" style="width: 100%">
        <el-table-column label="封面" width="150px">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" :preview-src-list="scope.row.cover.images"
              style="width: 100px; height: 100px" fit="cover">
              <div slot="error" class="image-slot">
                <img src="@/assets/image/Jerry1.png" width="100px" height="100px" alt="">
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="150px" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.status === 0">草稿</el-tag>
            <el-tag v-if="scope.row.status === 1">待审核</el-tag>
            <el-tag type="success" v-if="scope.row.status === 2">审核通过</el-tag>
            <el-tag type="danger" v-if="scope.row.status === 3">审核失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间" width="180px" />
        <el-table-column prop="read_count" label="阅读数" />
        <el-table-column prop="comment_count" label="评论数" />
        <el-table-column prop="like_count" label="点赞数" />
        <el-table-column label="操作" width="110px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle plain @click="$router.push(`/publish/${scope.row.id}`)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle plain @click="delArticle(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination style="margin-top: 20px;" :current-page.sync="reqParams.page" :page-sizes="[2, 5, 10]"
        :page-size.sync="reqParams.per_page" background :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper" @size-change="getArticle" @current-change="getArticle">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ArticlePage',
  data () {
    return {
      reqParams: {
        status: null,
        channel_id: null,
        begin_pubdate: null,
        end_pubdate: null,
        page: null,
        per_page: 2
      },
      // 频道列表
      channelOptions: [],
      // 日期数组
      dateArr: [],
      articleList: [],
      totalCount: 0

    }
  },
  methods: {
    getArticle () {
      this.$http.get('/v1_0/mp/articles', { params: this.reqParams }).then(res => {
        this.articleList = res.data.data.results
        this.totalCount = res.data.data.total_count
        console.log(res)
      })
    },
    sift () {
      this.reqParams.page = 1
      this.getArticle()
    },
    delArticle (id) {
      this.$confirm('确认删除该文章?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$http.delete(`/v1_0/mp/articles/${id}`)
        this.getArticle()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
      })
    }

  },
  watch: {
    dateArr: {
      deep: true,
      handler (value) {
        if (value === null) {
          this.reqParams.begin_pubdate = null
          this.reqParams.end_pubdate = null
        } else {
          this.reqParams.begin_pubdate = value[0]
          this.reqParams.end_pubdate = value[1]
        }
      }
    }
  },
  created () {
    // 获取文章列表
    this.getArticle()
  }
}
</script>

<style scoped lang="less"></style>
